//    Travel US - For users of travel.usairways.com
//    intended for US Airways employees and their dependents
//    This app will help you track your flights and display
//    Availability, flight status and other non-rev info
//    Copyright (C) 2014  Renats Stozkovs
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.

package com.bc004346.travelus.model.domain;

import android.support.annotation.NonNull;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by Renats on 1/20/14.
 * Reservation class
 */
public class Reservation implements Comparable<Reservation> {

    //<editor-fold desc="Members">

    private String pnr;
    private List<Flight> flights;
    private Date departure;
    private String bookingType;
    private List<Passenger> passengers;

    //</editor-fold>
    //<editor-fold desc="Constructor">

    public Reservation(String pnr, Date departure, String bookingType) {
        setPnr(pnr);
        setFlights(new ArrayList<Flight>());
        setDeparture(departure);
        setBookingType(bookingType);
        setPassengers(new ArrayList<Passenger>());
    }

    //</editor-fold>
    //<editor-fold desc="Properties">

    public String getPnr() {
        return pnr;
    }

    public void setPnr(String pnr) {
        this.pnr = pnr;
    }

    public List<Flight> getFlights() {
        return flights;
    }

    public void setFlights(List<Flight> flights) {
        this.flights = flights;
    }

    public Date getDeparture() {
        return departure;
    }

    void setDeparture(Date departure) {
        this.departure = departure;
    }

    public String getBookingType() {
        return bookingType;
    }

    void setBookingType(String bookingType) {
        this.bookingType = bookingType;
    }

    public List<Passenger> getPassengers() {
        return passengers;
    }

    public void setPassengers(List<Passenger> passengers) {
        this.passengers = passengers;
    }

    //</editor-fold>
    //<editor-fold desc="Overrides">

    @Override
    public String toString() {
        return String.format("PNR %s, departure date %s",
                this.getPnr(),
                new SimpleDateFormat("MM/dd/yy").format(getDeparture()));
    }

    @Override
    public int compareTo(@NonNull Reservation another) {
        if (this.getPnr().equals(another.getPnr())
                && this.getDeparture().equals(another.getDeparture())
                && this.getFlights().size() == another.getFlights().size()) {
            boolean match = true;
            for (int i = 0; i < this.getFlights().size(); i++) {
                if (this.getFlights().get(i).compareTo(another.getFlights().get(i)) != 0)
                    match = false;
            }
            for (int i = 0; i < this.getPassengers().size(); i++) {
                if (this.getPassengers().get(i).compareTo(another.getPassengers().get(i)) != 0)
                    match = false;
            }
            if (match) return 0;
        }

        if (this.getDeparture().before(another.getDeparture())) return -1;
        else return 1;
    }

    //</editor-fold>
}
